<?php

namespace Home\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;


class ThongtinTable extends AbstractTableGateway {

    protected $table = 'thongtin';

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Thongtin());

        $this->initialize();
    }

    public function fetchAll(Select $select = null) {
        if (null === $select)
            $select = new Select();
        $select->from($this->table,array('*'));
        $resultSet = $this->selectWith($select);
        $row = $resultSet->current(); 
        return $row;
    }

    public function getThongtin($id) {
        $id = (int) $id;
        $rowset = $this->select(array('id' => $id));
        $row = $rowset->current(); 
        return $row;
    }

    public function saveThongtin(Thongtin $thongtin) {
		
			
		
        

        $id = (int) $thongtin->id;
        if ($id == 0) {
			$data = array(
				'banner' => $thongtin->banner,
				'footer' => $thongtin->footer,
				'email' => $thongtin->email,
				'yahoo' => $thongtin->yahoo,
				'skype' => $thongtin->skype,
				
			);
            	
                $this->insert($data);
            
        } else {
			
			$data = array(
				'banner' => $thongtin->banner,
				'footer' => $thongtin->footer,
				'email' => $thongtin->email,
				'yahoo' => $thongtin->yahoo,
				'skype' => $thongtin->skype,
				
			);
            if ($this->getThongtin($id)) {
				
					$this->update($data, array('id' => $id));
				
            } else {
                throw new \Exception('Không tồn tại');
            }
        }
    }

    public function deleteThongtin($id) {
        $this->delete(array('id' => $id));
    }
	public function xoaThongtin($id) {
		
        $this->delete('id IN (' . $id . ')');
    }
	
	//kiem tra ton tai
	
	


}
